using UnityEngine;
using System.Collections;

public class V3Math : MonoBehaviour {

    public static float gravityConstant = 1f;
    public static float forceCuttoff = 0.1f;
    public static Vector3 Subtract(Vector3 a, Vector3 b)
    {
        Vector3 ret = Vector3.zero;
        ret.x = a.x - b.x;
        ret.y = a.y - b.y;
        ret.z = a.z - b.z;
        return ret;
    }
    public static Vector3 Add(Vector3 a, Vector3 b)
    {
        Vector3 ret = Vector3.zero;
        ret.x = a.x + b.x;
        ret.y = a.y + b.y;
        ret.z = a.z + b.z;
        return ret;
    }
    public static Vector3 Gravity(Vector3 source, Vector3 target, float strength)
    {
        Vector3 positionDiff = Subtract(source,target);
        float magnitudeSqr = positionDiff.sqrMagnitude;
        float force = (gravityConstant * strength / magnitudeSqr);
        if (force < forceCuttoff)
        {
            return Vector3.zero;
        }
        return positionDiff.normalized * force;
    }
}
